package ups.MapParse;

import java.util.ArrayList;
import java.util.Hashtable;

public class SpellCheck {
	public ArrayList<PositionedString> words;
	public ArrayList<String> guesses;
	public Hashtable<String, int[]> ranking;
	public ArrayList<String> out;
	ArrayList< StringTransform > resultTransforms;
	public SpellCheck( ArrayList<PositionedString> w, ArrayList<String> g) {
		words = w;
		guesses = g;
		ranking = new Hashtable< String, int[] >();
		ArrayList<StringTransform> possibleTransforms = new ArrayList<StringTransform>();
		
		for( PositionedString src : words ) {
			System.out.println( src.toString() );
			for( String dst : guesses ) {
				possibleTransforms.add( new StringTransform( src, dst ));
			}
		}
		
		resultTransforms = new ArrayList< StringTransform >();
		
		//for( PositionedString str : words ) {
			//System.out.println( str.toString() );
		//}
		
		int index = words.size();
		while( index-- != 0 ) {
			
		//	int pt;
			//System.out.println( "Possible Transforms:");
			//for( pt = 0 ; pt < possibleTransforms.size(); pt++ ) {
			//	 System.out.println( possibleTransforms.get( pt ));
			//}
			
			StringTransform tran = nextLikelyResult( possibleTransforms );
			//System.out.println("The Best Was " + tran.toString() );
			resultTransforms.add( tran );
			

			
			/* Now we have to remove all of the other ones that could turn into this */
			int transformIndex;
		//	System.out.println("Removing all that match either G or P");
			for( transformIndex = possibleTransforms.size()-1; transformIndex >= 0; transformIndex-- ) {
				StringTransform t = possibleTransforms.get( transformIndex );
				//System.out.println("Checking " + t.toString() );
				String sourceString = t.getSource().getString();
				String tranSourceString = tran.getSource().getString();
				if( t.getDestination().equals( tran.getDestination())) {
					//System.out.println("\tremoving...");
					possibleTransforms.remove( t );
				} else if( sourceString.equals( tranSourceString )) {
					 int size = possibleTransforms.size();
					 possibleTransforms.remove( t );
					// System.out.println("\tremoving...");
				}
			}
		}
	}
	
	public StringTransform nextLikelyResult( ArrayList<StringTransform> possibleTransforms ) {
		int lowest_distance = Integer.MAX_VALUE;
		
		StringTransform result = null;
		//System.out.println("");
		for( StringTransform tran : possibleTransforms ) {
		//	System.out.println( tran.toString() );
			if( tran.distance() < lowest_distance ) {
				lowest_distance = tran.distance();
				result = tran;
			}
		}
		
		//System.out.println("\n\n");
	//	System.out.println( "Best transformation was " + result.toString() );
		return result;
	}
	
	public ArrayList< StringTransform > getTransforms()  {
		return resultTransforms;
	}
	
	public void apply() {
		for( StringTransform s : resultTransforms ) {
			//System.out.println( "Using transform " + s.toString() + " for string" + s.getSource().toString() );
			s.getSource().setString( s.getDestination() );
		}
	}
}
